package com.yunbiao.ybbusstopplate.ui.dialog

import android.os.Handler
import android.os.Looper
import com.blankj.utilcode.util.ActivityUtils
import com.yunbiao.ybbusstopplate.base.APP
import com.yunbiao.ybbusstopplate.serialport.SerialPortConfig

class LetterErrorDialogTool {
    companion object{
        private var letterErrorDialog:LetterErrorDialog?=null
        private var handler:Handler?=null

        fun showLetterErrorDialog(title:String){
            if (handler==null){
                handler=Handler(Looper.getMainLooper())
            }
            handler!!.post {
                if (letterErrorDialog != null && letterErrorDialog!!.isShowing) {
                    letterErrorDialog!!.dismiss()
                    letterErrorDialog = null
                }
                val topActivity = ActivityUtils.getTopActivity()
                if(topActivity != null){
//                letterErrorDialog= LetterErrorDialog(APP.getHomeActivity(),SerialPortConfig.PORT_NAME,title)
                    letterErrorDialog = LetterErrorDialog(
                        topActivity,
                        SerialPortConfig.PORT_NAME,
                        title
                    )
                    letterErrorDialog!!.show()
                }
            }
        }

        fun showLetterErrorDialog(){
            showLetterErrorDialog("")
        }

        fun dismissLetterErrorDialog(){
            if (handler==null){
                handler=Handler(Looper.getMainLooper())
            }
            handler!!.post {
                if (letterErrorDialog != null && letterErrorDialog!!.isShowing) {
                    letterErrorDialog!!.dismiss()
                    letterErrorDialog = null
                }
            }
        }
    }
}